package com.duobaoyu.dby.supply.job.aspect;

import cn.hutool.core.util.IdUtil;
import com.duobaoyu.dby.common.core.constant.RequestConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

/**
 * @author fengshaoyu
 */
@Slf4j
@Aspect
@Component
public class RequestAspect {

	@Pointcut("@annotation(com.xxl.job.core.handler.annotation.XxlJob)")
	public void logger() {

	}

	@Around("logger()")
	public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
		try {
			String requestId = MDC.get(RequestConstants.REQUEST_ID);
			if(StringUtils.isEmpty(requestId)){
				MDC.put(RequestConstants.REQUEST_ID,IdUtil.nanoId());
			}
			return proceedingJoinPoint.proceed();
		} finally {
			MDC.remove(RequestConstants.REQUEST_ID);
		}
	}
}
